package dao;

import common.Information;
import common.User;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;

public class GetInformation {
    public ArrayList<Information> getInformation(User u) {
        ArrayList<Information> friends= new ArrayList<Information>();
        Connection conn=null;
        ResultSet res=null;
        try {
            Properties info=new Properties();
            info.load(new FileInputStream("src\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            //注册驱动
            Class.forName(Driver);
            //获取连接
            conn= DriverManager.getConnection(url,user,password);
            String sql="select * from friendlist where friend_id = ? and statement = ?";
            PreparedStatement presta = conn.prepareStatement(sql);
            presta.setString(1, u.getId());
            presta.setInt(2,0);
            res=presta.executeQuery();
            while(res.next()) {
                String fid=res.getString("user_id");
                Information newf=new Information();
                newf.setUserid(fid);
                newf.setType(0);
                friends.add(newf);
            }

            //return friends;
        }catch(SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return friends;
    }

    public ArrayList<Information> getGroupinformation(String groupid, ArrayList<Information> ilist) {
        ArrayList<Information> friends= new ArrayList<Information>();
        Connection conn=null;
        ResultSet res=null;
        try {
            Properties info=new Properties();
            info.load(new FileInputStream("src\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            //注册驱动
            Class.forName(Driver);
            //获取连接
            conn=DriverManager.getConnection(url,user,password);
            String sql="select * from grouplist where  group_id = ? and statement =  ?";
            PreparedStatement presta = conn.prepareStatement(sql);
            presta.setString(1, groupid);
            // presta.setString(2,);
            presta.setInt(2,0);
            res=presta.executeQuery();
            while(res.next()) {
                String fid=res.getString("member_id");
                //System.out.println(fid);
                //System.out.println("11111");
                Information newf=new Information();
                newf.setUserid(fid);
                newf.setType(0);
                newf.setGroupid(groupid);
                ilist.add(newf);
            }

            //return friends;
        }catch(SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ilist;
    }
}
